﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace jQuery.WebControls
{
    [Description("")]
    [ToolboxData("<{0}:GridView2 runat=server></{0}:GridView2>")]    
    public class GridView2 : GridView
    {
        private const string _virtualItemCount = "virtualItemCount";
        private const string _currentPageIndex = "currentPageIndex";

        [Browsable(true), Category("Custom")]
        [Description("Set the virtual item count for this grid")]
        public int VirtualItemCount
        {
            get
            {
                if (ViewState[_virtualItemCount] == null)
                    ViewState[_virtualItemCount] = -1;
                return Convert.ToInt32(ViewState[_virtualItemCount]);
            }
            set
            {
                ViewState[_virtualItemCount] = value;
            }
        }

        private int CurrentPageIndex
        {
            get
            {
                if (ViewState[_currentPageIndex] == null)
                    ViewState[_currentPageIndex] = 0;
                return Convert.ToInt32(ViewState[_currentPageIndex]);
            }
            set
            {
                ViewState[_currentPageIndex] = value;
            }
        }

        public override object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                base.DataSource = value;
                this.CurrentPageIndex = this.PageIndex;
            }
        }

        public string TotalRecordsCssClass { get; set; }

        public bool TotalRecordsDisplay { get; set; }

        public string TotalRecordsFormat { get; set; }

        protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
        {            
            pagedDataSource.AllowCustomPaging = CustomPaging;
            pagedDataSource.VirtualCount = this.VirtualItemCount;
            pagedDataSource.CurrentPageIndex = this.CurrentPageIndex;            
            base.InitializePager(row, columnSpan, pagedDataSource);
        }

        private bool CustomPaging
        {
            get { return (this.VirtualItemCount != -1); }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            if (TotalRecordsDisplay && DataSource != null)
            {
                string recordsFormat = (String.IsNullOrEmpty(TotalRecordsFormat) ? "{0:#,###}" : TotalRecordsFormat);
                int total = (this.VirtualItemCount <= 0 ? this.Rows.Count : this.VirtualItemCount);

                recordsFormat = String.Format(recordsFormat, total);

                writer.Write(String.Format("<div class='{0}'><span>{1}</span></div>", TotalRecordsCssClass, recordsFormat));
            }
        }
    }
}
